/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.ka.entity.BasicGoods;
import com.kaaction.modules.ka.entity.ProductManage;
import com.kaaction.modules.sys.entity.Dict;
import com.kaaction.modules.ka.dao.BasicGoodsDao;
import com.kaaction.modules.ka.dao.KaColorDao;
import com.kaaction.modules.ka.dao.KaSizeDao;

/**
 * 货品基础信息Service
 * @author huanghaiming
 * @version 2017-04-24
 */
@Service
@Transactional(readOnly = true)
public class BasicGoodsService extends CrudService<BasicGoodsDao, BasicGoods> {
	
	@Autowired
	KaSizeDao kaSizeDao;
	
	@Autowired
	KaColorDao kaColorDao;

	public BasicGoods get(String id) {
		return super.get(id);
	}
	
	public List<BasicGoods> findList(BasicGoods basicGoods) {
		return super.findList(basicGoods);
	}
	
	public Page<BasicGoods> findPage(Page<BasicGoods> page, BasicGoods basicGoods) {
		return super.findPage(page, basicGoods);
	}
	
	@Transactional(readOnly = false)
	public void save(BasicGoods basicGoods) {
		
		String[] sizeIds = basicGoods.getSids();//尺码
		String[] colorIds = basicGoods.getCids();//颜色
		String[] canalIds = basicGoods.getCaids();//渠道
		
		String sId = "";
		String cId = "";
		String sizeId="";
		String colorId="";
		String canalId="";
		
		//尺码字符串拼接
		if(sizeIds!=null){
			for(int i=0;i<sizeIds.length;i++){
				sId+= kaSizeDao.selectId(sizeIds[i])+",";
				sizeId+=sizeIds[i]+",";
			}
			basicGoods.setSizeName(sId.substring(0, sId.length()-1));
			basicGoods.setSizeId(sizeId.substring(0, sizeId.length()-1));
		}
		//颜色字符串拼接
		if(colorIds!=null){
			for(int k=0;k<colorIds.length;k++){
				cId+= kaColorDao.selectId(colorIds[k])+",";
				colorId+=colorIds[k]+",";
			}
			basicGoods.setColorName(cId.substring(0, cId.length()-1));
			basicGoods.setColorId(colorId.substring(0, colorId.length()-1));
		}
		//渠道字符串拼接
		if(canalIds!=null&&canalIds.length>0){
			for(int j=0;j<canalIds.length;j++){
				canalId+=canalIds[j]+",";
			}
			basicGoods.setCanalId(canalId.substring(0, canalId.length()-1));
		}
		
		super.save(basicGoods);
	}
	
	@Transactional(readOnly = false)
	public void delete(BasicGoods basicGoods) {
		super.delete(basicGoods);
	}

	@Transactional(readOnly = false)
	public void deleteAll(BasicGoods basicGoods) {
	
		String[] ids = basicGoods.getIds();
		for(String id : ids){
			basicGoods.setId(id);
			super.delete(basicGoods);
		}
		
	}

	public int getEqual(BasicGoods basicGoods) {
		return super.dao.getEqual(basicGoods);
	}

	public List<Dict> getProductCode(ProductManage productManage) {
		return super.dao.getProductCode(productManage);
	}

	public BasicGoods getAttr(ProductManage productManage) {
		return  super.dao.getAttr(productManage);
	} 
	
}